import Vue from 'vue'
import App from './App'
import LoginRegister from "@/components/LoginRegister";
import HelloWorld from "@/components/HelloWorld";
import Blog from '@/components/Blog.vue'
import Home from "@/components/Home";
import Create from "@/components/Create";
import ChatRoom from "@/components/ChatRoom"
import PostDetail from "@/components/PostDetail";
import Search from "@/components/Search";
import Index from "@/components/index"
import Room from "@/components/room"
import Module from "@/components/module"
import RoomChatList from "@/components/RoomChatList";
import PersonalInfo from "@/components/PersonalInfo";
import CreatRoles from "@/components/CreatRoles";
import UploadModule from "@/components/UploadModule";
import DownloadModule from "@/components/DownloadModule";
import CreateCharacter from "@/components/CreateCharacter";

import VueResource from 'vue-resource'
import VuePaginate from 'vue-paginate'
import VueRouter from 'vue-router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'bootstrap'
import 'jquery'
import axios from 'axios'
import qs from 'qs'
import VueCookies from 'vue-cookies'
import './vue.config.js'
import {options} from "vue-resource/src/util";
// import SockJS from  'sockjs-client';
// import Stomp from 'stompjs';

//请求的根路径
axios.defaults.baseURL="http://127.0.0.1:8081/"
Vue.prototype.$socket_path='';

// Vue.prototype.$http=axios

Vue.config.productionTip = false

// ENABLE USE 3RD PARTY PLUGINS
Vue.use(VueResource);
Vue.use(VuePaginate);
Vue.use(VueRouter);
Vue.use(ElementUI);
Vue.use(VueCookies);
// Vue.use(SockJS);
// Vue.use(Stomp);

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    // 判断是否存在token,如果存在将每个页面header添加token
    if(sessionStorage.getItem('token')!==null){
        config.headers.common['Authorization'] = "Bearer " + sessionStorage.getItem('token');
    }
    return config
}, function (error) {
    router.push('/')
    return Promise.reject(error)
})

// Vue.http.interceptors.push((request, next) => {
//     //请求发送前的处理逻辑
//     request.headers.set('Authorization', "Bearer " + sessionStorage.getItem('token'))
//     if(sessionStorage.getItem('token')!==null){
//         Vue.http.headers.common['Authorization'] = "Bearer " + sessionStorage.getItem('token');
//     }
//     next((response) => {
//         //请求发送后的处理逻辑
//         //根据请求的状态，response参数返回给successCallback或errorCallback
//         return response
//     })
// })

// ENABLE SINGLE PAGE APP ROUTING
const routes = [
    {path: '/post/:id', component: PostDetail},
    {path: '/', component: LoginRegister},
    {
        path: '/CreateCharacter',
        component: CreateCharacter,
        meta: {
            isLogin: false    // 添加该字段，表示进入这个路由是需要登录的
        }//路由元
    },
    {
        path: '/HelloWorld',
        component: HelloWorld,
        meta: {
            isLogin: true    // 添加该字段，表示进入这个路由是需要登录的
        }//路由元
    },
    {
        path: '/DownloadModule',
        component: DownloadModule,
        meta: {
            isLogin: false    // 添加该字段，表示进入这个路由是需要登录的
        }//路由元
    },
    {
        path: '/Blog',
        component: Blog,
        meta: {
            isLogin: false    // 添加该字段，表示进入这个路由是需要登录的
        }//路由元
    },
    {
        path: '/blog',
        component: Blog,
        meta: {
            isLogin: false    // 添加该字段，表示进入这个路由是需要登录的
        }//路由元
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        meta: {
            isLogin: false
        },
        children: [
            {
                path: 'personalInfo',
                name: 'PersonalInfo',
                component: PersonalInfo,
            },
            {
                path: 'uploadModule',
                name: 'UploadModule',
                component: UploadModule,
            },
            {
                path: 'creatRoles',
                name: 'CreatRoles',
                component: CreatRoles,
            }
        ],
    },
    {
        path: '/create',
        component: Create, meta: {
            isLogin: false
        }
    },
    {
        path: '/search',
        component: Search, meta: {
            isLogin: false
        }
    },
    {
        path: '/room',
        component: Room, meta: {
            isLogin: false
        },
        children: [
            {
                path: 'roomchatlist',
                component: RoomChatList,
                meta: {},
                children: [
                    {
                        path: 'chatroom',
                        name: 'chatroom',
                        // component: resolve => void(require(['../components/customTask.vue'], resolve)),
                        component: ChatRoom,
                        meta: {},
                    }
                ]
            },

        ]
    },
    {
        path: '/index',
        component: Index, meta: {
            isLogin: false
        }
    },
    {
        path: '/module',
        component: Module, meta: {
            isLogin: false
        }
    },
    // {path: '/' , component: App,
    //   children:[
    //     {path:'/Blog',component:Blog},
    //   ]}
];

const router = new VueRouter({
    routes,
    type: history
})

// INIT APP
new Vue({
    el: '#app',
    router,
    render: h => h(App)
})

//挂载路由守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/') return next();
    if (to.meta.isLogin) {//判断是否需要登录
        if (sessionStorage.getItem('Token') !== null) {
            next();
        } else {
            next({
                path: "/"
            });
        }
    } else {
        next();
    }
})